﻿Imports Oracle.DataAccess.Client
Imports lbGlobalcom
Public Class fListaBodegas
    Private daSedes As lbGlobalcom.dsGlobalTableAdapters.GLO_SEDESTableAdapter
    Private daBodegas As lbGlobalcom.dsInventariosTableAdapters.INV_BODEGASTableAdapter
    Private lIdBodega As String = ""
    Private lIdsede As String = ""
    Private lDescripcion As String = ""
    Private lFiltraBySede As Boolean = True
    Public Property FiltraBySede() As Boolean
        Get
            Return lFiltraBySede
        End Get
        Set(ByVal value As Boolean)
            lFiltraBySede = value
        End Set
    End Property
    Public ReadOnly Property Bodega() As String
        Get
            Return lIdBodega
        End Get
    End Property

    Public ReadOnly Property pDescrip() As String
        Get
            Return lDescripcion
        End Get
    End Property
    Public WriteOnly Property idSede() As String
        Set(ByVal value As String)
            lIdsede = value
        End Set
    End Property
    Private Sub CargaSedes()
        Try
            daSedes.Fill(DsGlobal.GLO_SEDES)
            Dim Dr As lbGlobalcom.dsGlobal.GLO_SEDESRow = DsGlobal.GLO_SEDES.NewGLO_SEDESRow
            Dr.IDSEDE = "-1"
            Dr.NOMBRE = "Todas"
            DsGlobal.GLO_SEDES.Rows.Add(Dr)
            Dr = Nothing
            If lIdsede.Trim.Length > 0 And lFiltraBySede Then
                cbSedes.SelectedValue = lIdsede
                cbSedes.Enabled = False
            Else
                cbSedes.SelectedValue = "-1"
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Samuel")
        End Try
    End Sub
    Private Sub fListaBodegas_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        daSedes = New lbGlobalcom.dsGlobalTableAdapters.GLO_SEDESTableAdapter
        daBodegas = New lbGlobalcom.dsInventariosTableAdapters.INV_BODEGASTableAdapter
        Try
            CargaSedes()
            Me.cbEstado.Text = "Todas"
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Samuel")
        End Try
    End Sub
    Private Function Estado(ByVal Est As String) As String
        Select Case Est
            Case "Activa"
                Return "S"
            Case "Inactiva"
                Return "N"
            Case Else
                Return ""
        End Select
    End Function

    Private Sub consultar()
        Dim Conn As OracleConnection = New OracleConnection(My.Settings.ConnectionString)
        Dim cmd As OracleCommand = Conn.CreateCommand
        Dim CadSql As String = "SELECT * FROM INV_BODEGAS "
        Dim qWhere As String = "WHERE "
        Dim CuentaW As Int16 = 0
        If Me.txtNombreBod.Text.Trim.Length > 0 Then
            CuentaW += 1
            qWhere &= "DESCRIPCION LIKE '%" & txtNombreBod.Text & "%'"
        End If
        If Me.cbSedes.SelectedValue <> "-1" Then
            If CuentaW > 0 Then _
                qWhere &= " AND "
            CuentaW += 1
            qWhere &= "IDSEDE = " & cbSedes.SelectedValue
        End If
        If Estado(cbEstado.SelectedValue).Trim.Length > 0 Then
            If CuentaW > 0 Then _
                qWhere &= " AND "
            CuentaW += 1
            qWhere &= "ACTIVA = " & Estado(cbEstado.SelectedValue)
        End If
        If CustomPrincipal.IDSede.Length > 0 Then
            If CuentaW > 0 Then _
                qWhere &= " AND "
            CuentaW += 1

            qWhere &= CustomPrincipal.GetFiltroSede("IDSEDE")
        End If

        If CuentaW > 0 Then _
            CadSql &= qWhere
        Try
            cmd.CommandText = CadSql
            Conn.Open()
            DsInventarios.INV_BODEGAS.Rows.Clear()
            DsInventarios.INV_BODEGAS.Load(cmd.ExecuteReader)
            Conn.Close()
            INV_BODEGASDataGridView.Focus()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Samuel")
        End Try
    End Sub

    Private Sub brnConsulta_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles brnConsulta.Click
        Try
            consultar()
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    
    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub

    Private Sub INV_BODEGASDataGridView_CellMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles INV_BODEGASDataGridView.CellMouseDoubleClick
        If e.RowIndex >= 0 Then
            lIdBodega = INV_BODEGASDataGridView.Rows(e.RowIndex).Cells("IDBODEGA").Value
            lDescripcion = INV_BODEGASDataGridView.Rows(e.RowIndex).Cells("DESCRIPCION").Value
            Me.DialogResult = Windows.Forms.DialogResult.OK
        End If
    End Sub

    Private Sub AsignaBodega(ByVal Fila As Integer)
        If Fila >= 0 Then
            lIdBodega = INV_BODEGASDataGridView.Rows(Fila).Cells("IDBODEGA").Value
            lDescripcion = INV_BODEGASDataGridView.Rows(Fila).Cells("DESCRIPCION").Value
            Me.DialogResult = Windows.Forms.DialogResult.OK
        End If
    End Sub

    Private Sub INV_BODEGASDataGridView_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles INV_BODEGASDataGridView.KeyDown
        If e.KeyData = Keys.Enter Then
            AsignaBodega(INV_BODEGASDataGridView.CurrentRow.Index)
        End If
    End Sub

    Private Sub txtNombreBod_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtNombreBod.KeyDown
        Try
            Select Case e.KeyCode
                Case 13
                    consultar()
            End Select
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
End Class